﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//@author A0083726Y
namespace EZXPNS
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
        }

        private void Search_Click(object sender, EventArgs e)
        {
            //Clear listView before searching
            listView1.Items.Clear();

            if (textBox1.Text == "")
            {
                MessageBox.Show("Nothing is entered!");
                return;
            }

            if (EzXpns._incomeList.Count == 0 && EzXpns._expenseList.Count == 0)
            {
                MessageBox.Show("No income and expense items!");
                return;
            }

            string searchTerms;
            searchTerms = textBox1.Text;

            string[] indivSearchTerms = searchTerms.Split(' ');

            //stores no. of matches per item
            int[] incomeItemAdded;
            incomeItemAdded = new int[EzXpns._incomeList.Count];
            int[] expenseItemAdded;
            expenseItemAdded = new int[EzXpns._expenseList.Count];

            //Loop through _incomeList
            for (int i = 0; i < indivSearchTerms.Length; i++)
            {

                for (int j = 0; j < EzXpns._incomeList.Count; j++)
                {
                    if (EzXpns._incomeList[j].Name.ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._incomeList[j].Amount.ToString().ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._incomeList[j].Category.ToLower().Contains(indivSearchTerms[i].ToLower())
                        //  || EzXpns._incomeList[j].Date.ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._incomeList[j].Payment.ToLower().Contains(indivSearchTerms[i].ToLower())
                        )
                    {
                        incomeItemAdded[j]++;
                    }
                }

                //Loop through _expenseList
                for (int m = 0; m < EzXpns._expenseList.Count; m++)
                {
                    if (EzXpns._expenseList[m].Name.ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._expenseList[m].Amount.ToString().ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._expenseList[m].Category.ToLower().Contains(indivSearchTerms[i].ToLower())
                        //|| EzXpns._expenseList[m].Date.ToLower().Contains(indivSearchTerms[i].ToLower())
                        || EzXpns._expenseList[m].Payment.ToLower().Contains(indivSearchTerms[i].ToLower())
                        )
                    {
                        expenseItemAdded[m]++;
                    }
                }
            }


            for (int i = indivSearchTerms.Length; i >= 1; i--)
            {
                for (int j = 0; j < EzXpns._incomeList.Count; j++)
                    if (incomeItemAdded[j] == i)
                    {
                        ListViewItem lvi = new ListViewItem("Income");
                        lvi.SubItems.Add(EzXpns._incomeList[j].Name);
                        lvi.SubItems.Add(EzXpns._incomeList[j].Amount.ToString());
                        lvi.SubItems.Add(EzXpns._incomeList[j].Category);
                        lvi.SubItems.Add(EzXpns._incomeList[j].Date);
                        lvi.SubItems.Add(EzXpns._incomeList[j].Payment);
                        listView1.Items.Add(lvi);
                    }

                for (int m = 0; m < EzXpns._expenseList.Count; m++)
                    if (expenseItemAdded[m] == i)
                    {
                        ListViewItem lvi = new ListViewItem("Expense");
                        lvi.SubItems.Add(EzXpns._expenseList[m].Name);
                        lvi.SubItems.Add(EzXpns._expenseList[m].Amount.ToString());
                        lvi.SubItems.Add(EzXpns._expenseList[m].Category);
                        lvi.SubItems.Add(EzXpns._expenseList[m].Date);
                        lvi.SubItems.Add(EzXpns._expenseList[m].Payment);
                        listView1.Items.Add(lvi);
                    }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SearchPower _powerSearch = new SearchPower();
            _powerSearch.ShowDialog();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Confirm exit?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
                this.Close();

            else
            {
                //do nothing}
            };
        }
    }
}
